package com.aaron.demo1.common.action;

import com.aaron.demo1.common.MyRedisLock;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import java.util.concurrent.TimeUnit;

/**
 *
 * @author liurong
 * @date 2021/7/12 18:51
 */
@RestController
@RequestMapping("/bigSale")
public class BigSaleAction {

    public static final String PRODUCT_NAME = "phone";
    public static final int PRODUCT_STOCK = 100;

    @Autowired
    RedissonClient redissonClient;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @PostConstruct
    public void setProductStock() {
        stringRedisTemplate.opsForValue().set(PRODUCT_NAME, String.valueOf(PRODUCT_STOCK));
    }

    @GetMapping("/reset")
    public String reset(String stockNum) {
        stringRedisTemplate.opsForValue().set(PRODUCT_NAME, String.valueOf(stockNum));
        Integer stock = Integer.valueOf(stringRedisTemplate.opsForValue().get(PRODUCT_NAME));
        String result = "{\"stock\":" + stock + "}";
        System.out.println(result);
        return result;
    }

    @GetMapping("/sell")
    public String sellProduct() {

        String lockName = PRODUCT_NAME + "_lock";
        RLock rLock = redissonClient.getLock(lockName);

        String result = "";

        try {
            rLock.lock(30, TimeUnit.SECONDS);
            Integer stock = Integer.valueOf(stringRedisTemplate.opsForValue().get(PRODUCT_NAME));

            if (stock > 0) {
                int realStock = stock - 1;
                stringRedisTemplate.opsForValue().set(PRODUCT_NAME, String.valueOf(realStock));
                result = "抢购成功，库存量：" + realStock;
            } else {
                result = "抢购失败，库存不足";
            }

        } finally {
            rLock.unlock();
        }
        System.out.println(result);
        return "{\"message\":\"" + result + "\"}";

    }

    @GetMapping("/sell2")
    public String sellProduct2() {

        MyRedisLock lock = new MyRedisLock(PRODUCT_NAME, stringRedisTemplate);
        String result = "";

        try {
            lock.lock(30, TimeUnit.SECONDS);
            if(!lock.holdLock()){
                System.out.println("请重试！");
                return "请重试！";
            }
            Integer stock = Integer.valueOf(stringRedisTemplate.opsForValue().get(PRODUCT_NAME));

            if (stock > 0) {
                int realStock = stock - 1;
                stringRedisTemplate.opsForValue().set(PRODUCT_NAME, String.valueOf(realStock));
                result = "抢购成功，库存量：" + realStock;
            } else {
                result = "抢购失败，库存不足";
            }

        } finally {
            lock.unLock();
        }
        System.out.println(result);
        return "{\"message\":\"" + result + "\"}";

    }


}
